﻿using System;
using System.Buffers;
using System.Linq;
using System.Text;

using Microsoft.Extensions.DependencyInjection;

namespace Xc.DataAccess.PcpChannels
{
    /// <summary>
    /// 设备请求升级包
    /// </summary>
    public class DevicePackageRequest : RespondFrame
    {
        /// <summary>
        /// 设备请求升级包
        /// </summary>
        /// <param name="dataarea"></param>
        public DevicePackageRequest(byte[] dataarea) : base(dataarea)
        {

        }

        /// <summary>
        /// 要校验的数据
        /// </summary>
        /// <returns></returns>
        protected override MessageCode? ValidateMessageCode => MessageCode.PackageRequest;
        /// <summary>
        /// 目标版本号
        /// </summary>
        public string PackageVersion { get; private set; }
        /// <summary>
        /// 分片序号
        /// </summary>
        public ushort PackageIndex { get;private set; }
        /// <summary>
        /// 数据域数据
        /// </summary>
        protected override bool SetDataArea(ref System.Buffers.SequenceReader<byte> dataArea)
        {
            if (dataArea.TryRead(16, out var c))
            {
                PackageVersion = Encoding.ASCII.GetString(c.ToArray().Where(p=>p!=0).ToArray());
                if (dataArea.TryReadUInt16BigEndian(out var index))
                {
                    PackageIndex = index;
                }
                else
                {
                    return false;
                }
            }
            return false;
        }
    }
}
